<?php
    require_once("core/functions.php"); //includes the core functions of the system
    core::getResource("Controller.RegisterController"); //already includes database class
    $error = null;
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        if (isset($_POST["firstName"]) && isset($_POST["lastName"]) && isset($_POST["email"]) && isset($_POST["contactNumber"]) && isset($_POST["password"])) {
            $firstName = core::sanitize($_POST["firstName"]);
            $lastName = core::sanitize($_POST["lastName"]);
            $email = core::sanitize($_POST["email"]);
            $contactNumber = core::sanitize($_POST["contactNumber"]);
            $password = core::sanitize($_POST["password"]);
            $gender = $_POST["gender"];
            $accountType = ACC_VOLUNTEER;
            //$firstName,$lastName,$email,$password,gender,$contactNumber,$accountType
            $regger = new RegisterController($firstName, $lastName, $email, $password, $gender, $contactNumber, $accountType);
            $result = $regger->register();
            switch ($result) {
                case REGISTER_SUCCESS:
                    $message = "Your Activation Key is : <h2>";
                    $message .= $regger->getActivationKey();
                    $message .= "</h2><br/>";
                    $message .= "Thank You for registering!";
                    $message .= "<br/>We Welcome you to UTS Volunteering Portal, ";
                    $message .= $firstName;
                    $message .= "<br/><br/>Login in to the UTS:V website and enter this activation key";
                    $message .= "<br/><br/>";
                    $message .= "UTS:V Admin";
                    core::sendMail($email,"UTS:V Account Activation",$message);
                    core::redirect("index.php?registration=sucess");
                    break;
                case REG_ERR:
                    $error = REG_ERR;
                    break;
                case REG_ERR_EMAIL:
                    $error = REG_ERR_EMAIL;
                    break;
            }
        }
    }
?>
<?php $title = "Register"; ?>
<?php include_once("header.php"); ?>
<?php
    if($loggedIn)
    {
        Core::redirect("index.php");
    }
?>
<div class="centerContent">
    <div class="pageHeading">Register</div>
    <div id="regForm">
        <form id="registrationForm" METHOD="POST" ACTION="<?= $_SERVER['PHP_SELF']; ?>"  
              onsubmit="return UTSDK.VALIDATION.validate('registrationForm')">
            <table>
                <tr>
                    <td><label>First Name: </label></td>
                    <td><input id="firstName" type="text" name="firstName" placeholder="First name" 
                               data-validation="name" data-maxlength="35"/></td>
                    <td><label id="firstNameError"></label></td>
                </tr>
                <tr>
                    <td><label>Last Name: </label></td>
                    <td><input id="lastName" type="text" name="lastName" placeholder="Last name" 
                               data-validation="name" data-maxlength="35"/></td>
                    <td><label id="lastNameError"></label></td>
                </tr>
                <tr>
                    <td><label>Email: </label></td>
                    <td><input id="email" type="email" name="email" placeholder="email@domain.com" 
                               data-validation="email" data-validationmessage="Not a valid email"/></td>
                    <td><label id="emailError"></label></td>
                </tr>
                <tr>
                    <td><label>Gender: </label></td>
                    <td><div class="selectDiv">
                            <select id="gender" name="gender">
                                <option value="M">Male</option>
                                <option value="F">Female</option>
                                <option value="O">Other</option>
                            </select>
                        </div></td>
                    <td><label id="genderError"></label></td>
                </tr>
                <tr>
                    <td><label>Contact Number: </label></td>
                    <td><input id="contactNumber" type="text" name="contactNumber" placeholder="Contact number" 
                               data-validation="number" data-validationmessage="Not a valid contact number"
                               data-maxlength="20"/></td>
                    <td><label id="contactNumberError"></label></td>
                </tr>
                <tr>
                    <td><label>Password: </label></td>
                    <td><input id="password" type="password" name="password" placeholder="Password" 
                               data-validation="password" data-validationmessage="Invalid password"/></td>
                    <td><label id="passwordError"></label></td>
                </tr>
                <tr>
                    <td></td>
                    <td colspan="2" class="info">Password must be 8 characters long, have 1 capital and 1 number</td>
                </tr>
                <tr>
                    <td><label>Re-Password: </label></td>
                    <td><input id="rePassword" type="password" name="rePassword" placeholder="Retype password" 
                               data-validation="password" data-validationmessage="Invalid password"
                               data-match="password" data-matchmessage="Passwords do not match"/></td>
                    <td><label id="rePasswordError"></label></td>
                </tr>
                <tr>
                    <td></td>
                    <td><input class="submitBtn" type="submit" value="Register" id="rSubmit"/></td>
                    <td></td>
                </tr>
            </table>
        </form>
    </div>
</div>
<?php include_once("footer.php"); ?>